<template>
	<view class="content">
		<view class="navbar">
			<view v-for="(item, index) in navList" :key="index" class="nav-item" :class="{current: tabCurrentIndex == index}"
			 @click="tabClick(index)">
				{{item.text}}
			</view>
		</view>

		<!-- 订单列表 -->
		<view v-for="(item,index) in orderList" :key="index" class="order-item">
		
			<view class="i-top b-b">
				<text class="time" v-if="item.buyUserInfo">买家 : {{item.buyUserInfo}}({{item.orderSn}})</text>
				<text class="time" v-else>订单编号 : {{item.orderSn}}</text>
				<text :class="item.orderStatus == 103 ? 'state' : item.orderStatus == 301 ? 'stateTwo' : 'stateOne'">{{item.orderStatusText}}</text>
			</view>
		
			<view class="goods-box-single" v-for="(goodsItem, goodsIndex) in item.goodsList" :key="goodsIndex" @click="toDetail(item.orderStatus,item.id,item.userId)">
				<image class="goods-img" :src="goodsItem.picUrl" mode="aspectFill"></image>
				<view class="right">
					<text class="title clamp">{{goodsItem.goodsName}}</text>
					<text class="attr-box">{{goodsItem.specifications[0]}} {{goodsItem.specifications[1]}} x {{goodsItem.number}}</text>
					<text class="number">{{goodsItem.number}}</text>
					<text class="price">{{goodsItem.price}}</text>
				</view>
			</view>
		
			<view class="action-box b-t" v-if="item.orderStatus == 201">
				<button class="action-btn" @click="toDetail(item.orderStatus,item.id)">再次购买</button>
			</view>
			
			<view class="action-box b-t" v-if="item.orderStatus == 101">
				<button class="action-btn" @click="toDetail(item.orderStatus,item.id)">取消订单</button>
				<button class="action-btn recom" @click="toDetail(item.orderStatus,item.id)">立即付款</button>
			</view>
			
			<view class="action-box b-t" v-if="item.orderStatus == 103">
				<button class="action-btn" @click="toDetail(item.orderStatus,item.id)">删除订单</button>
			</view>
			
			<view class="action-box b-t" v-if="item.orderStatus == 301">
				<button class="action-btn" @click="toDetail(item.orderStatus,item.id)">查看物流</button>
				<button class="action-btn recom" @click="toDetail(item.orderStatus,item.id)">确认收货</button>
			</view>
		
		</view>
		<!-- 空白页 -->
		<empty v-if="orderList.length === 0"></empty>
		
		
		<!--加载loadding-->
		<tui-loadmore v-if="loadding" :index="total" type="primary"></tui-loadmore>
		<tui-nomore v-if="!pullUpOn"></tui-nomore>
		<!--加载loadding-->
	</view>
</template>

<script>
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
	import empty from "@/components/fenxiao.vue";
	import Json from '@/Json';
	export default {
		components: {
			uniLoadMore,
			empty
		},
		data() {
			return {
				scrollTop: 0,
				userId: "",						//用户
				tabCurrentIndex: 0, 			//订单状态
				navList: [
					{
						state: 0,
						text: '全部订单',
					},
					{
						state: 1,
						text: '待付款',
					},
					{
						state: 2,
						text: '待发货',
					},
					{
						state: 3,
						text: '待收货',
					},
					{
						state: 4,
						text: '评价',
					},
				],

				total: "",
				limit: "10", //一页十条
				page: "1", //几页
				pages: "", //一共多少页
				loadding: false, //加载
				pullUpOn: true, //没有数据
				orderList: [],
				orderType:"",
			};
		},
		// 页面滚动执行方式
		onPageScroll(e) {
			this.scrollTop = e.scrollTop
		},

		// 页面加载
		onLoad(options) {
			console.log(options)
			let userInfo = uni.getStorageSync("userInfo")
			this.userId = userInfo.id
			this.tabCurrentIndex = options.state;
			let stateOrder = options.id;
			let title = '我的云仓订单';
			this.orderType = options.orderType
			if (stateOrder === '1') {
				title = '客户云仓订单'
				this.orderType = options.orderType
			}else if(stateOrder === '3'){
				title = '分销订单'
				this.orderType = options.orderType
			}
			uni.setNavigationBarTitle({
				title
			})
			this.loadData()
		},

		methods: {
			// 下拉刷新
			onPullDownRefresh: function() {
				this.page = 1;
				this.pullUpOn = true;
				this.loadding = false;
				
				let _this = this;
				console.log(_this.orderType)
				_this.tui.request('/app/order/list', 'GET', {
							userId: _this.userId,
							page: _this.page,
							limit: _this.limit,
							orderType: _this.orderType,
							showType: _this.tabCurrentIndex,
						},
						false, false, false, )
					.then(res => {
						if (res.errno == 0) {
							_this.total = res.data.total
							_this.pages = res.data.pages
							_this.orderList = res.data.list
							
						} else {
							console.log(res)
							_this.tui.toast(res.errmsg)
						}
					}).catch(err => {
						console.log(err)
					})
				
				
				uni.stopPullDownRefresh();
				this.tui.toast("刷新成功")
			},

			// 上拉加载
			onReachBottom: function() {
				console.log(this.page)
				if (!this.pullUpOn) {
					return;
				}
				this.loadding = true;
				if (this.page == this.pages) {
					this.loadding = false;
					this.pullUpOn = false
				} else {
					++this.page;
					this.loadData();
					this.loadding = false
				}
			},

			// 获取订单列表
			loadData: function() {
				let _this = this;
				console.log(_this.orderType)
				_this.tui.request('/app/order/list', 'GET', {
							userId: _this.userId,
							page: _this.page,
							limit: _this.limit,
							orderType: _this.orderType,
							showType: _this.tabCurrentIndex,
						},
						false, false, false, )
					.then(res => {
						if (res.errno == 0) {
							_this.total = res.data.total
							_this.pages = res.data.pages
							let worksList = res.data.list;
							worksList.forEach(item => {
								_this.orderList.push(item);
							});
						} else {
							console.log(res)
							_this.tui.toast(res.errmsg)
						}
					}).catch(err => {
						console.log(err)
					})
			},

			//订单状态文字和颜色
			orderStateExp(state) {
				let stateTip = '',
					stateTipColor = '#fa436a';
				switch (+state) {
					case 101:
						stateTip = '待付款';
						break;
					case 102:
						stateTip = '已取消';
						stateTipColor = '#909399';
						break;
					case 103:
						stateTip = '已取消(系统)';
						stateTipColor = '#909399';
						break;
					case 201:
						stateTip = '已付款';
						break;
					case 202:
						stateTip = '订单取消，退款中';
						break;
					case 203:
						stateTip = '已退款';
						stateTipColor = '#909399';
						break;
					case 204:
						stateTip = '已超时团购';
						break;
					case 301:
						stateTip = '已发货';
						break;
					case 401:
						stateTip = '已收货';
						break;
					case 402:
						stateTip = '已收货(系统)';
						break;
				}
				return {
					stateTip,
					stateTipColor
				};
			},

			
			//顶部tab点击
			tabClick(index) {
				this.tabCurrentIndex = index;
				let _this = this;
				console.log(_this.orderType)
				_this.tui.request('/app/order/list', 'GET', {
							userId: _this.userId,
							page: _this.page,
							limit: _this.limit,
							orderType: _this.orderType,
							showType: _this.tabCurrentIndex,
						},
						false, false, false, )
					.then(res => {
						if (res.errno == 0) {
							_this.total = res.data.total
							_this.pages = res.data.pages
							_this.orderList = res.data.list
							
						} else {
							console.log(res)
							_this.tui.toast(res.errmsg)
						}
					}).catch(err => {
						console.log(err)
					})
			},

			// 跳转详情
			toDetail(index, row, userId) {
				uni.setStorageSync("orderId", row);
				
				uni.navigateTo({
					url: `/pages/order/orderDetail?id=${index}&orderId=${row}&userId=${userId}`
				})
			}
		},
	}
</script>

<style lang="scss">
	page,
	.content {
		background: $page-color-base;
		height: 100%;
	}

	.swiper-box {
		height: calc(100% - 40px);
	}

	.list-scroll-content {
		height: 100%;
	}

	.navbar {
		display: flex;
		height: 40px;
		padding: 0 5px;
		background: #fff;
		box-shadow: 0 1px 5px rgba(0, 0, 0, .06);
		position: relative;
		z-index: 10;

		.nav-item {
			flex: 1;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100%;
			font-size: 12px;
			color: #808080;
			position: relative;

			&.current {
				color: #333333;

				&:after {
					content: '';
					position: absolute;
					left: 50%;
					bottom: 12px;
					-webkit-transform: translateX(-50%);
					transform: translateX(-50%);
					width: 45px;
					height: 0px;
					border-bottom: 5px solid #6E609E;
					opacity: .8;
					border-radius: 5px;
				}
			}
		}
	}

	.uni-swiper-item {
		height: auto;
	}

	.order-item {
		display: flex;
		flex-direction: column;
		padding-left: 30upx;
		background: #fff;
		margin-top: 16upx;

		.i-top {
			display: flex;
			align-items: center;
			height: 80upx;
			padding-right: 30upx;
			font-size: $font-base;
			color: #333333;
			position: relative;

			.time {
				flex: 1;
				color: #333333;
			}

			.state {
				color: #FF0000;
			}
			.stateOne {
				color: #333333;
			}
			.stateTwo {
				color: #6E609E;
			}

			.del-btn {
				padding: 10upx 0 10upx 36upx;
				font-size: $font-lg;
				color: $font-color-light;
				position: relative;

				&:after {
					content: '';
					width: 0;
					height: 30upx;
					border-left: 1px solid $border-color-dark;
					position: absolute;
					left: 20upx;
					top: 50%;
					transform: translateY(-50%);
				}
			}
		}

		/* 多条商品 */
		.goods-box {
			height: 160upx;
			padding: 20upx 0;
			white-space: nowrap;

			.goods-item {
				width: 120upx;
				height: 120upx;
				display: inline-block;
				margin-right: 24upx;
			}

			.goods-img {
				display: block;
				width: 100%;
				height: 100%;
			}
		}

		/* 单条商品 */
		.goods-box-single {
			display: flex;
			padding: 20upx 0;

			.goods-img {
				display: block;
				width: 120upx;
				height: 120upx;
			}

			.right {
				flex: 1;
				display: flex;
				flex-direction: column;
				padding: 0 30upx 0 24upx;
				overflow: hidden;

				.title {
					font-size: $font-base + 2upx;
					color: $font-color-dark;
					line-height: 1;
				}

				.attr-box {
					font-size: $font-sm + 2upx;
					color: $font-color-light;
					padding: 10upx 12upx;
				}

				.number {
					font-size: 12px;
					color: #999999;
					text-align: right;

					&:before {
						content: '￥';
						font-size: $font-sm;
						margin: 0 2upx 0 8upx;
					}
				}

				.price {
					font-size: $font-base + 2upx;
					color: #D14798;
					text-align: right;

					&:before {
						content: '￥';
						font-size: $font-sm;
						margin: 0 2upx 0 8upx;
					}
				}
			}
		}

		.price-box {
			display: flex;
			justify-content: flex-end;
			align-items: baseline;
			padding: 20upx 30upx;
			font-size: $font-sm + 2upx;
			color: $font-color-light;

			.num {
				margin: 0 8upx;
				color: $font-color-dark;
			}

			.price {
				font-size: $font-lg;
				color: $font-color-dark;

				&:before {
					content: '￥';
					font-size: $font-sm;
					margin: 0 2upx 0 8upx;
				}
			}
		}

		.action-box {
			display: flex;
			justify-content: flex-end;
			align-items: center;
			height: 100upx;
			position: relative;
			padding-right: 30upx;
		}

		.action-btn {
			width: 160upx;
			height: 60upx;
			margin: 0;
			margin-left: 24upx;
			padding: 0;
			text-align: center;
			line-height: 60upx;
			font-size: $font-sm + 2upx;
			color: $font-color-dark;
			background: #fff;
			border-radius: 100px;

			&:after {
				border-radius: 100px;
			}
		}
	}


	/* load-more */
	.uni-load-more {
		display: flex;
		flex-direction: row;
		height: 80upx;
		align-items: center;
		justify-content: center
	}

	.uni-load-more__text {
		font-size: 28upx;
		color: #999
	}

	.uni-load-more__img {
		height: 24px;
		width: 24px;
		margin-right: 10px
	}

	.uni-load-more__img>view {
		position: absolute
	}

	.uni-load-more__img>view view {
		width: 6px;
		height: 2px;
		border-top-left-radius: 1px;
		border-bottom-left-radius: 1px;
		background: #999;
		position: absolute;
		opacity: .2;
		transform-origin: 50%;
		animation: load 1.56s ease infinite
	}

	.uni-load-more__img>view view:nth-child(1) {
		transform: rotate(90deg);
		top: 2px;
		left: 9px
	}

	.uni-load-more__img>view view:nth-child(2) {
		transform: rotate(180deg);
		top: 11px;
		right: 0
	}

	.uni-load-more__img>view view:nth-child(3) {
		transform: rotate(270deg);
		bottom: 2px;
		left: 9px
	}

	.uni-load-more__img>view view:nth-child(4) {
		top: 11px;
		left: 0
	}

	.load1,
	.load2,
	.load3 {
		height: 24px;
		width: 24px
	}

	.load2 {
		transform: rotate(30deg)
	}

	.load3 {
		transform: rotate(60deg)
	}

	.load1 view:nth-child(1) {
		animation-delay: 0s
	}

	.load2 view:nth-child(1) {
		animation-delay: .13s
	}

	.load3 view:nth-child(1) {
		animation-delay: .26s
	}

	.load1 view:nth-child(2) {
		animation-delay: .39s
	}

	.load2 view:nth-child(2) {
		animation-delay: .52s
	}

	.load3 view:nth-child(2) {
		animation-delay: .65s
	}

	.load1 view:nth-child(3) {
		animation-delay: .78s
	}

	.load2 view:nth-child(3) {
		animation-delay: .91s
	}

	.load3 view:nth-child(3) {
		animation-delay: 1.04s
	}

	.load1 view:nth-child(4) {
		animation-delay: 1.17s
	}

	.load2 view:nth-child(4) {
		animation-delay: 1.3s
	}

	.load3 view:nth-child(4) {
		animation-delay: 1.43s
	}

	@-webkit-keyframes load {
		0% {
			opacity: 1
		}

		100% {
			opacity: .2
		}
	}
</style>
